/*
 * Copyright 2003,2004 The Apache Software Foundation
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package eu.jakubiak.jpa.translator.locale;


import java.util.Locale;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 
 * [language[_country][.variant]
 * http://en.wikipedia.org/wiki/Locale
 * 
 * @author jakubiak_a
 */



public class LocaleParser {
    private static Pattern pattern = Pattern.compile("([a-z]{2,3})(([_-]([A-Z]{2,3}))([.](.*))?)?");


    private Locale locale = null;

    private String language = null;
    private String country = null;
    private String variant = null;
    
    
    public LocaleParser(String localeString) {
        Matcher matcher =  pattern.matcher(localeString);
        if(matcher.matches()) {
            language = matcher.group(1);
            country = matcher.group(4);
            variant = matcher.group(6);
            if(language!=null&&country!=null&&variant!=null) {
                locale = new Locale(language, country, variant);
            } else if(language!=null&&country!=null) {
                locale = new Locale(language, country);
            } else if(language!=null) {
                locale = new Locale(language);
            }
        }
        
    }

    public String getCountry() {
        return country;
    }

    public String getLanguage() {
        return language;
    }

    public Locale getLocale() {
        return locale;
    }

    public String getVariant() {
        return variant;
    }
}
